package com.dss.auto_deploy.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dss.auto_deploy.domain.AdmProject;
import com.dss.auto_deploy.domain.AdmProjectAutoConfig;
import com.dss.auto_deploy.dto.AdmProjectAutoConfigAddDto;
import com.dss.auto_deploy.dto.AdmProjectAutoConfigPageDto;
import com.dss.auto_deploy.dto.AdmProjectAutoConfigUpdateDto;
import com.dss.auto_deploy.dto.GitlabPushDto;
import com.dss.auto_deploy.enums.StateEnum;
import com.dss.auto_deploy.mapper.AdmProjectAutoConfigMapper;
import com.dss.auto_deploy.mapper.AdmProjectMapper;
import com.dss.auto_deploy.service.IAdmProjectAutoConfigService;
import com.dss.auto_deploy.service.IGitlabPushService;
import com.dss.auto_deploy.util.BeanUtils;
import com.dss.auto_deploy.util.EnumUtils;
import com.dss.auto_deploy.util.JacksonUtils;
import com.dss.auto_deploy.util.StringUtils;
import com.dss.auto_deploy.vo.AdmProjectAutoConfigVo;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
* @Description:配置自动触发更新的项目/分支Service实现类
* @author dongshoushan
* @date 2022-10-22 14:13:37
*/
@Service
@Slf4j
@RequiredArgsConstructor(onConstructor = @_(@Autowired))
public class AdmProjectAutoConfigServiceImpl  extends ServiceImpl<AdmProjectAutoConfigMapper,AdmProjectAutoConfig> implements IAdmProjectAutoConfigService {

    /**
    *
    * 配置自动触发更新的项目/分支Mapper
    */
    private final AdmProjectAutoConfigMapper admProjectAutoConfigMapper;
    private final AdmProjectMapper admProjectMapper;
    private final IGitlabPushService gitlabPushService;
    /**
    * 新增配置自动触发更新的项目/分支
    * @param admProjectAutoConfigAddDto
    * @return Long
    */
    @Override
    public int insert(AdmProjectAutoConfigAddDto admProjectAutoConfigAddDto){
        AdmProjectAutoConfig admProjectAutoConfig = BeanUtils.copy(admProjectAutoConfigAddDto, AdmProjectAutoConfig.class);
        admProjectAutoConfig.setUpdateTime(new Date());
        int insert = admProjectAutoConfigMapper.insert(admProjectAutoConfig);
        if (insert == 1) {
            log.info("新增成功{}",JacksonUtils.toString(admProjectAutoConfig));
        }
        return insert;
    }


    /**
    * 修改配置自动触发更新的项目/分支
    * @param admProjectAutoConfigUpdateDto
    * @return Long
    */
    @Override
    public int update(AdmProjectAutoConfigUpdateDto admProjectAutoConfigUpdateDto){
        AdmProjectAutoConfig admProjectAutoConfig = BeanUtils.copy(admProjectAutoConfigUpdateDto, AdmProjectAutoConfig.class);
        admProjectAutoConfig.setUpdateTime(new Date());
        int number = admProjectAutoConfigMapper.updateById(admProjectAutoConfig);
        if (number == 1) {
            log.info("修改成功{}",JacksonUtils.toString(admProjectAutoConfig));
        }
        return number;
    }


    /**
    * 批量删除配置自动触发更新的项目/分支
    * @param ids
    * @return Long
    */
    @Override
    public void deleteBatch(Long[] ids){
        this.removeByIds(Arrays.asList(ids));
    }

    /**
    * 配置自动触发更新的项目/分支分页查询
    *
    * @param pageDto
    * @return List<AdmProjectAutoConfig>
    */
    @Override
    public Page<AdmProjectAutoConfigVo> list(AdmProjectAutoConfigPageDto pageDto){
        Page<AdmProjectAutoConfigVo> tablesPage = new Page<>(pageDto.getPage(), pageDto.getLimit());
        List<AdmProjectAutoConfigVo> voList = admProjectAutoConfigMapper.list(pageDto,tablesPage);
        tablesPage.setRecords(voList);
        voList.forEach(admProjectAutoConfigVo -> admProjectAutoConfigVo.setStateName(EnumUtils.getDescByCode(StateEnum.class, admProjectAutoConfigVo.getState())));
        return tablesPage;
    }

    /**
     * 手动触发器
     *
     * @param id 主键id
     */
    @Override
    public void manualTrigger(Long id) {
        AdmProjectAutoConfig admProjectAutoConfig = this.getById(id);
        Long projectId = admProjectAutoConfig.getProjectId();
        String branch = admProjectAutoConfig.getBranch();
        AdmProject admProject = admProjectMapper.selectById(projectId);
        GitlabPushDto gitlabPushDto = new GitlabPushDto();
        GitlabPushDto.Project project = GitlabPushDto.Project.builder().name(admProject.getGitlabProjectName()).url(admProject.getGitUrl()).build();
        gitlabPushDto.setProject(project);
        gitlabPushDto.setObject_kind("push");
        gitlabPushDto.setRef(StringUtils.format("refs/heads/{}", branch));
        gitlabPushDto.setCheckout_sha("111");
        //List<GitlabPushDto.CommitsItem> commits = new ArrayList<>();
        //commits.add(GitlabPushDto.CommitsItem.builder()
        //        .build());
        //gitlabPushDto.setCommits(commits);
        gitlabPushService.toDeployDocker(gitlabPushDto);
    }


}
